package com.sto.stoapp.thread;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.db.manager.UploadDataManager;
import com.sto.stoapp.http.SignHelpSmsHttpManager;
import com.sto.stoapp.http.request.upload.SignHelpSmsInfo;
import com.sto.stoapp.http.request.upload.SignHelpSmsRequest;
import com.sto.stoapp.http.response.SignHelpSmsResponse;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.upload.UploadDataService;
import com.sto.stoapp.util.Helper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wj on 2016-12-07.
 *
 * 代收签收短信接口
 */

public class SignHelpSmsThread extends BaseThread{

    private Context context;
    private Handler handler;
    private EnumTableNames status;

    public SignHelpSmsThread(Context context, Handler handler, EnumTableNames status) {
        super(context);
        this.context = context;
        this.handler = handler;
        this.status = status;
    }

    @Override
    public void run() {
        super.run();
        List<Object> objects = UploadDataManager.getInstance(context)
                .queryUploadData(status);
        if (objects == null || objects.size() == 0) {
            handler.obtainMessage(status.ordinal(), UploadDataService.NO_DATAS)
                    .sendToTarget();
            return;
        }

         String response = "";

        try {

            List<SignHelpSmsInfo> infos=new ArrayList<SignHelpSmsInfo>();
            for(int i=0;i<objects.size();i++){
                SignHelpSmsInfo info= (SignHelpSmsInfo) objects.get(i);
                infos.add(info);
            }

            SignHelpSmsRequest request=new SignHelpSmsRequest();
            request.orgCode= StoFilePreference.getInstance(context).getSiteCode();
            request.sessionId=StoFilePreference.getInstance(context).getSessionId();
            request.tid=StoFilePreference.getInstance(context).getBarCodeNumber();
            request.opUserCode=StoFilePreference.getInstance(context).getUserCode();
            request.data=infos;

            Gson gson=new Gson();

            String requestStr=gson.toJson(request);
            Log.e("wj",requestStr);

            SignHelpSmsHttpManager mgr = new SignHelpSmsHttpManager(context);
            response = mgr.dopost(requestStr);
            //处理返回数据
            SignHelpSmsResponse responseDate=gson.fromJson(response, SignHelpSmsResponse.class);

            //成功
            if("0".equals(responseDate.result)){
                //Toast.makeText(context,responseDate.message,Toast.LENGTH_SHORT).show();
                //成功后发送状态
                handler.obtainMessage(
                        EnumTableNames.STATUS_TABLE_SALESMAN_HELP_SIGN.ordinal(),
                        UploadDataService.SUCCESS).sendToTarget();
            }else {
                handler.obtainMessage(
                        EnumTableNames.STATUS_TABLE_SALESMAN_HELP_SIGN
                                .ordinal(),
                        UploadDataService.ERROR).sendToTarget();
            }

        } catch (JsonSyntaxException e1) {
            e1.printStackTrace();
            ExceptionLog.writeLog("SignHelpSmsThread ",
                    "SignHelpSmsThread 解析出错：" + response
                            + " /n Exception:" + e1.toString());
            showMsg("上传数据服务器返回数据解析出错");
            handler.obtainMessage(
                    EnumTableNames.STATUS_TABLE_SALESMAN_HELP_SIGN
                            .ordinal(),
                    UploadDataService.ERROR).sendToTarget();
            return;
        }catch (Exception e){
            ExceptionLog.writeLog("SignHelpSmsThread ",
                    "SignHelpSmsThread 解析出错：" + response
                            + " /n Exception:" + e.toString());
            showMsg("上传数据服务器返回数据解析出错");
            handler.obtainMessage(
                    EnumTableNames.STATUS_TABLE_SALESMAN_HELP_SIGN
                            .ordinal(),
                    UploadDataService.ERROR).sendToTarget();
            return;
        }
    }

    private void showMsg(final String text) {
        handler.post(new Runnable() {

            @Override
            public void run() {
                Helper.showToast(context, text);
            }
        });
    }
}
